package org.morningangel.servlet.android;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.morningangel.android.bean.RemindBean;
import org.morningangel.bean.Remind;
import org.morningangel.bean.User;
import org.morningangel.dao.RemindDao;
import org.morningangel.dao.UserDao;
import org.morningangel.dao.impl.RemindDaoImpl;
import org.morningangel.dao.impl.UserDaoImpl;

@WebServlet( name="AndroidUpdateRemindTimeServlet", urlPatterns="/android/remindTime")
public class UpdateRemindTimeServlet extends HttpServlet {
	private static final long serialVersionUID = -3174793020611438661L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	    Integer userId  = Integer.parseInt(request.getParameter("userId"));
	    Integer rTime=Integer.parseInt(request.getParameter("remindTime"));
	    RemindBean remindBean=new RemindBean();
	    Remind remind=null;
	    RemindDao remindDao=new RemindDaoImpl();
	    PrintWriter pWriter=response.getWriter();
	    Integer b=0;
	    if(rTime!=0){//执行为更新操作
	    	b=remindDao.updateRemindTime(userId, rTime);
	    	pWriter.write("{'isSuccess':'3'}");
	    	System.out.println("{'isSuccess':'3'}");
	    	pWriter.flush();
	    	pWriter.close();
	    }else{//执行获取提醒时间
	    	remind=remindDao.getRemindTimeById(userId);
	    	remindBean=new RemindBean(remind);
	    	JSONObject jObj=JSONObject.fromObject(remindBean);
	        pWriter.write(jObj.toString());
	        pWriter.flush();
	        pWriter.close();
	    }
	   
	}
}
